package com.hengtongxing.hejiayun.homepage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.bean.PayFeeListBean;
import com.hengtongxing.hejiayun.utils.DateTimeUtil;
import com.hengtongxing.hejiayun.utils.NumberFormatUtils;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 咨询师列表
 */
public class PayFeeListAdapter extends RecyclerView.Adapter<PayFeeListAdapter.ViewHolder> {


    private List<PayFeeListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public PayFeeListAdapter(List<PayFeeListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(PayFeeListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_pay_fee_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PayFeeListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getMoney()) / 100);
        String price = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getPrice()) / 100);
        holder.tvMoney.setText("￥" + money);
        holder.tvHouse.setText(dataBean.getMy_hoses().getComm_name() + dataBean.getMy_hoses().getBuilding() + dataBean.getMy_hoses().getUnit_name() + dataBean.getMy_hoses().getFloor() + dataBean.getMy_hoses().getHose_number());
        holder.tvMonth.setText("" + dataBean.getValid_month());
        holder.tvHouseArea.setText(dataBean.getArea() + "㎡");
        holder.tvPrice.setText(price + "元/平方米 每月");
        //1未支付 2已支付
        holder.tvBtnPay.setVisibility(View.GONE);
        holder.llPayTime.setVisibility(View.GONE);
        if (dataBean.getPay_status() == 1) {
            holder.viewBg.setBackgroundResource(R.drawable.circle_orange_bg);
            holder.tvBtnPay.setVisibility(View.VISIBLE);
        } else if (dataBean.getPay_status() == 2) {
            holder.viewBg.setBackgroundResource(R.drawable.circle_theme_bg);
            holder.llPayTime.setVisibility(View.VISIBLE);
            holder.tvPayTime.setText(DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getPay_time()) * 1000)));
        }

        holder.tvBtnPay.setOnClickListener(view -> {
            onCallBackListener.onCallBack(position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_house)
        TextView tvHouse;
        @BindView(R.id.view_bg)
        View viewBg;
        @BindView(R.id.tv_month)
        TextView tvMonth;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_house_area)
        TextView tvHouseArea;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_btn_pay)
        TextView tvBtnPay;
        @BindView(R.id.tv_pay_time)
        TextView tvPayTime;
        @BindView(R.id.ll_pay_time)
        LinearLayout llPayTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int position);
    }
}
